package com.skycong.hello77.datepicker;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.skycong.hello77.R;
import com.skycong.hello77.datepicker.adapter.TopAndBottomPopAdapter;
import com.skycong.hello77.datepicker.entity.OwnTypeSelect;

import java.util.ArrayList;
import java.util.List;

/**
 * 顶部，底部弹窗 Pop模式
 * PopupWindow就是弹出窗口的意思，
 * 类似windows下面的开始按钮。
 * PopupWindow可以实现浮层效果，而且可以自定义显示位置，出现和退出时的动画.
 * Created by Kiki on 2018/6/21.
 */

public class TopAndBottomPop implements AdapterView.OnItemClickListener {
    private Context context;
    private View view;
    public PopupWindow popupWindow;//弹窗
    public List<OwnTypeSelect> ownTypeSelects  = new ArrayList<>();
    public onTopAndBottomPopLister listener;
    public TopAndBottomPopAdapter topAndBottomPopAdapter;
    private ListView listView;
    private View bg_view;

    public TopAndBottomPop(Context context,List<OwnTypeSelect> ownTypeSelects,onTopAndBottomPopLister listener){
        this.context = context;
        this.ownTypeSelects = ownTypeSelects;
        this.listener = listener;
        //用于PopupWindow的View
        view = LayoutInflater.from(context).inflate(R.layout.layout_pop_window_type,null);
        setPopSpec(view);
        initView(view);
    }

    private void initView(View viewCC) {
        listView = (ListView) viewCC.findViewById(R.id.listview);
        listView.setOnItemClickListener(this);
        bg_view = view.findViewById(R.id.bg_view);
        topAndBottomPopAdapter = new TopAndBottomPopAdapter(context,ownTypeSelects);
        listView.setAdapter(topAndBottomPopAdapter);
        topAndBottomPopAdapter.notifyDataSetChanged();
        //popupWindow下的view,空白页，点击就可以关闭popupWindow
        bg_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    /**
     * 设置弹窗的样式
     */

    public void setPopSpec(View viewContent) {
        //height要设置为MATCH_PARENT ,否则7.0系统或其他手机在弹输入法时该框不显示
        /*
        * 创建PopupWindow对象，其中：第一个参数是用于PopupWindow中的View，
        * 第二个参数是PopupWindow的宽度，第三个参数是PopupWindow的高度，
        * */
        popupWindow = new PopupWindow(viewContent,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT
                );
        //设置popWindow弹出窗可点击
        popupWindow.setFocusable(true);
        /*
        * 只有同时设置PopupWindow的背景和可以响应外部点击事件，
        * 它才能“真正”响应外部点击事件。也就是说，当你点击PopupWindow的外部或者按下“Back”键时，PopupWindow才会消失
        * */
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        //设置软键盘和软键盘调整size
        popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if(listener != null){
                    listener.onDismiss();
                }
            }
        });
    }

    /**
     * 设置弹窗的显示位置
     * 在哪里点击，就在那个位置的下面显示
     */
  public void setPopShow(View view){
      ViewGroup.LayoutParams params = listView.getLayoutParams();
      params.width = ViewGroup.LayoutParams.MATCH_PARENT;
      params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
      listView.setLayoutParams(params);
      try {
          if (Build.VERSION.SDK_INT >= 24) {
              Rect rect = new Rect();
              view.getGlobalVisibleRect(rect);
              int h = view.getResources().getDisplayMetrics().heightPixels - rect.bottom;
              popupWindow.setHeight(h);
          }
          popupWindow.showAsDropDown(view);
      }catch (Exception e){
          e.printStackTrace();
      }
  }

    /**
     * 设置在特定的位置，
     * 底部，顶部，中部
     * 不过布局中的<View></View>要对应着放
     */
  public void setPopShow(){
      //在底部显示
      try {
          popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }



    //弹出窗的item点击事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        OwnTypeSelect ownTypeSelect = ownTypeSelects.get(position);
        if(listener!=null){
            listener.onReciverItem(ownTypeSelect,position);
        }
        popupWindow.dismiss();
    }

    public interface onTopAndBottomPopLister{
        void onDismiss();
        void onReciverItem(OwnTypeSelect ownTypeSelect,int position);
    }
}
